package com.openharmony.filepicker.widget;

import ohos.agp.components.AttrSet;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.database.DataSetSubscriber;
import ohos.app.Context;

public class RecyclerViewFilePicker extends ListContainer {

    private Component emptyView = null;

    public RecyclerViewFilePicker(Context context) {
        super(context);
    }

    public RecyclerViewFilePicker(Context context, AttrSet attrSet) {
        super(context, attrSet);
    }

    public RecyclerViewFilePicker(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    public void setEmptyView(Component emptyView) {
        this.emptyView = emptyView;
        this.emptyView.setVisibility(Component.HIDE);
    }

    public boolean hasEmptyView() {
        return emptyView != null;
    }

    @Override
    public void setItemProvider(BaseItemProvider itemProvider) {
        super.setItemProvider(itemProvider);
        itemProvider.addDataSubscriber(new DataSetSubscriber() {
            @Override
            public void onChanged() {
                super.onChanged();
                if ((itemProvider == null || itemProvider != null && itemProvider.getCount() == 0) && emptyView != null) {
                    emptyView.setVisibility(Component.VISIBLE);
                    RecyclerViewFilePicker.this.setVisibility(Component.HIDE);
                } else {
                    if (emptyView != null) {
                        emptyView.setVisibility(Component.HIDE);
                    }
                    RecyclerViewFilePicker.this.setVisibility(Component.VISIBLE);
                }
            }

            @Override
            public void onInvalidated() {
                super.onInvalidated();
            }

            @Override
            public void onItemChanged(int position) {
                super.onItemChanged(position);
            }

            @Override
            public void onItemInserted(int position) {
                super.onItemInserted(position);
            }

            @Override
            public void onItemRemoved(int position) {
                super.onItemRemoved(position);
            }

            @Override
            public void onItemRangeChanged(int startPos, int countItems) {
                super.onItemRangeChanged(startPos, countItems);
            }

            @Override
            public void onItemRangeInserted(int startPos, int countItems) {
                super.onItemRangeInserted(startPos, countItems);
            }

            @Override
            public void onItemRangeRemoved(int startPos, int countItems) {
                super.onItemRangeRemoved(startPos, countItems);
            }
        });
    }
}
